﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using LuceneWrap.Lib;

namespace LuceneBasedStorage
{
    [Obsolete("For now this is put on hold... direct file system is used for Queues to reduce the locking time")]
    public class LuceneQueue 
    {
        class QueueItem
        {
            public string ID { get; set; }
            public object Item { get; set; }
        }

        string queueFolder;
        public LuceneQueue(string queueName)
        {
            // take the storage root folder 
            // create the new folder this queue.

            if (string.IsNullOrEmpty(queueFolder))
                throw new ApplicationException("Queue name is mandatory field");

            if (string.IsNullOrEmpty(LuceneStorageConfingHelper.StorageRootFolder))
                throw new ApplicationException("LuceneBasedStorage.StorageRootFolder name is mandatory confiuguration entry");


            queueFolder = Path.Combine(LuceneStorageConfingHelper.StorageRootFolder, queueName);

            if (!Directory.Exists(queueFolder))
            {
                Directory.CreateDirectory(queueFolder);
            }
        }

        public void EnQueue(object obj)
        {
            if (string.IsNullOrEmpty(queueFolder) || Directory.Exists(queueFolder))
                throw new ApplicationException("The Queue directory is not exists or not supplied");

            QueueItem queueItem = new QueueItem { ID = Guid.NewGuid().ToString("N"), Item = obj };

            LuceneWrap.Lib.LuceneManager<QueueItem> luceneManager = new LuceneWrap.Lib.LuceneManager<QueueItem>(queueFolder);

            luceneManager.AddObjectToIndex(queueItem);
            luceneManager.FinalizeWriter(true);
        }

        LuceneSearcher searcher;

        public object DeQueue()
        {
            if (string.IsNullOrEmpty(queueFolder) || Directory.Exists(queueFolder))
                throw new ApplicationException("The Queue directory is not exists or not supplied");

            if (searcher == null)
                searcher = new LuceneSearcher(queueFolder);

            if (searcher.MaxDoc() == 0)
                return null;

            QueueItem dequeuedObject = searcher.GetFristDoc<QueueItem>();

            if (dequeuedObject != null)
            {
                LuceneManager<QueueItem> luceneManager = new LuceneManager<QueueItem>(queueFolder);

                luceneManager.DeleteObjectFromIndexByField("id" , dequeuedObject.ID);

                luceneManager.FinalizeWriter(true);
            }

            //luceneManager.AddObjectToIndex(queueItem);

            return dequeuedObject.Item;
        }


        public int GetQueueCount()
        {
            if (searcher == null)
                searcher = new LuceneSearcher(queueFolder);

            return searcher.MaxDoc();
        }
    }
}
